using System;
using System.Diagnostics;
using System.Management;
namespace IcsSEMCtrl.Util
{
	public class LocalServerPerformanceData
	{
		private ManagementPath cpuPath;
		private ManagementPath memoryPath;
		private ManagementPath logicalDiskPath1;
		private ManagementPath logicalDiskPath2;
		private ManagementObject cpuObject;
		private ManagementObject memoryObject;
		private ManagementObject logicalDiskObject1;
		private ManagementObject logicalDiskObject2;
		private ManagementScope scope;
		private string path = "\\\\" + Environment.MachineName + "\\root\\cimv2";
		private ulong oldCpuValue;
		private ulong oldNanoValue;
		public LocalServerPerformanceData()
		{
			this.SetManagementScope();
			this.SetWMIOption();
		}
		private void SetManagementScope()
		{
			this.scope = new ManagementScope(this.path);
			try
			{
				this.scope.Connect();
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
			}
		}
		private void SetWMIOption()
		{
			this.cpuPath = new ManagementPath();
			this.cpuPath.RelativePath = "Win32_PerfRawData_PerfOS_Processor.Name='_total'";
			this.cpuObject = new ManagementObject(this.scope, this.cpuPath, null);
			this.memoryPath = new ManagementPath();
			this.memoryPath.RelativePath = "Win32_PerfRawData_PerfOS_Memory=@";
			this.memoryObject = new ManagementObject(this.scope, this.memoryPath, null);
			this.logicalDiskPath1 = new ManagementPath();
			this.logicalDiskPath1.RelativePath = "Win32_LogicalDisk.DeviceID='C:'";
			this.logicalDiskObject1 = new ManagementObject(this.scope, this.logicalDiskPath1, null);
			try
			{
				this.logicalDiskPath2 = new ManagementPath();
				this.logicalDiskPath2.RelativePath = "Win32_LogicalDisk.DeviceID='D:'";
				this.logicalDiskObject2 = new ManagementObject(this.scope, this.logicalDiskPath2, null);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
			}
		}
		public int ObtainCpuPerformance()
		{
			decimal value = 0m;
			int result;
			try
			{
				this.cpuObject.Get();
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				result = 0;
				return result;
			}
			ulong value2 = (ulong)this.cpuObject.Properties["PercentProcessorTime"].Value;
			ulong value3 = (ulong)this.cpuObject.Properties["TimeStamp_Sys100NS"].Value;
			decimal d = Convert.ToDecimal(value2);
			decimal d2 = Convert.ToDecimal(value3);
			decimal d3 = Convert.ToDecimal(this.oldCpuValue);
			decimal d4 = Convert.ToDecimal(this.oldNanoValue);
			try
			{
				value = (1m - (d - d3) / (d2 - d4)) * 100m;
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
			}
			this.oldCpuValue = value2;
			this.oldNanoValue = value3;
			result = Convert.ToInt32(value);
			return result;
		}
		public int ObtainMemoryPerformance()
		{
			int result;
			try
			{
				this.memoryObject.Get();
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.StackTrace.ToString());
				result = 0;
				return result;
			}
			ulong value = (ulong)this.memoryObject.Properties["AvailableMBytes"].Value;
			result = Convert.ToInt32(value);
			return result;
		}
		public int ObtainLogicalDisk1()
		{
			int result;
			try
			{
				this.logicalDiskObject1.Get();
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.StackTrace.ToString());
				result = 0;
				return result;
			}
			ulong num = (ulong)this.logicalDiskObject1.Properties["FreeSpace"].Value;
			ulong num2 = (ulong)this.logicalDiskObject1.Properties["Size"].Value;
			result = Convert.ToInt32(num * 100uL / num2);
			return result;
		}
		public int ObtainLogicalDisk2()
		{
			int result;
			try
			{
				this.logicalDiskObject2.Get();
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.StackTrace.ToString());
				result = 0;
				return result;
			}
			ulong num = (ulong)this.logicalDiskObject2.Properties["FreeSpace"].Value;
			ulong num2 = (ulong)this.logicalDiskObject2.Properties["Size"].Value;
			result = Convert.ToInt32(num * 100uL / num2);
			return result;
		}
		public bool ExistHardDiskCrashed()
		{
			EventLog[] eventLogs = EventLog.GetEventLogs();
			bool flag = false;
			bool flag2 = false;
			bool result;
			try
			{
				EventLog[] array = eventLogs;
				for (int i = 0; i < array.Length; i++)
				{
					EventLog eventLog = array[i];
					if (eventLog.Log == "Application")
					{
						EventLogEntryCollection entries = eventLog.Entries;
						for (int j = entries.Count - 1; j >= 0; j--)
						{
							EventLogEntry eventLogEntry = entries[j];
							if (eventLogEntry.Source.Contains("Adaptec Storage") && (eventLogEntry.EntryType == EventLogEntryType.Warning || eventLogEntry.EntryType == EventLogEntryType.Error))
							{
								if (eventLogEntry.InstanceId == 301L || eventLogEntry.InstanceId == 401L || eventLogEntry.InstanceId == 338L)
								{
									int gapTime1toNow = Helper.GetGapTime1toNow(Helper.DateFormat.HOUR, eventLogEntry.TimeGenerated);
									if (gapTime1toNow < 2)
									{
										flag = true;
										flag2 = true;
										break;
									}
								}
								else
								{
									if (eventLogEntry.Message.Contains("degraded") || eventLogEntry.Message.Contains("Failed"))
									{
										int gapTime1toNow = Helper.GetGapTime1toNow(Helper.DateFormat.HOUR, eventLogEntry.TimeGenerated);
										if (gapTime1toNow < 2)
										{
											flag = true;
											flag2 = true;
											break;
										}
									}
								}
							}
						}
						if (flag2)
						{
							break;
						}
					}
				}
				result = flag;
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.StackTrace.ToString());
				result = false;
			}
			return result;
		}
	}
}
